/**
 * @file usr_ntc.c
 * @author 黑马小乌龟 (532352406@qq.com)
 * @brief
 * @version 0.1
 * @date 2025-10-18
 *
 * @copyright Copyright (c) 2025
 *
 */
#include "usr_adc_handle.h"
#include "usr_ntc.h"

const int ntc_3380k_b_value[][2]={ 
/*temp(°C), Resistance (Ω)*/
{-40,195652},
{-35,148171},
{-30,113347},
{-25,87559},
{-20,68237},
{-15,53650},
{-10,42506},
{-5,33892},
{0,27219},
{5,22021},
{10,17926},
{15,14674},
{20,12081},
{25,10000},
{30,8315},
{35,6948},
{40,5834},
{45,4917},
{50,4161},
{55,3535},
{60,3014},
{65,2586},
{70,2228},
{75,1925},
{80,1669},
{85,1452},
{90,1268},
{95,1110},
{100,974},
{105,858},
{110,758},
{115,672},
{120,596},
{125,531},
};
#
// static uint8_t ntc_adc_read_process = 0;
#define NTC_RUP_ 10000ul  // NTC的上拉电阻
/**
 * @brief  Use ADC in polling mode.
 * @param  None
 * @retval None
 */
#if 0
int ntc_adc_polling(int *p_temperature) {
    uint32_t AdcValue, Rntc;
    int ret = -1;
    int i;
    int start_temp, range_temp, range_res, ntc_ratio;

    /* Can ONLY start sequence A conversion.
       Sequence B needs hardware trigger to start conversion. */
    if (ntc_adc_read_process == 0) {
        ntc_adc_read_process = 1;
        ADC_Start(NTC_ADC_UNIT);
        // DDL_Printf("NTC Adc start convert!!\n");
    } else {
        if (ADC_GetStatus(NTC_ADC_UNIT, NTC_ADC_EOC_FLAG) == SET) {
            ntc_adc_read_process = 0;
            ADC_ClearStatus(NTC_ADC_UNIT, NTC_ADC_EOC_FLAG);
            /* Get any ADC value of sequence A channel that needed. */
            AdcValue = ADC_GetValue(NTC_ADC_UNIT, NTC_ADC_CH);
            
            // Rntc=(adc_value*Rup)/(NTC_ADC_ACCURACY-adc_value)
            Rntc = (AdcValue * NTC_RUP_) / (NTC_ADC_ACCURACY - AdcValue);

            int rows = sizeof(ntc_3380k_b_value) / sizeof(ntc_3380k_b_value[0]);
           
            for (i = 0; i < rows; i++) {
                if (ntc_3380k_b_value[i][1] <= Rntc) {
                    break;
                }
            }

            // DDL_Printf("The ADC value of potentiometer is %u, voltage is %u mV ,Rntc:%d,row:%d, i:%d\n", \
            //     AdcValue, NTC_ADC_CAL_VOL(AdcValue),\
            //     Rntc,rows,i);
            if (i) {
                // 在 ResistanceTable 中找到 R_T 所在的位置区间，即找到 R_T 介于 RTable[i] 和 RTable[i+1] 之间。
                // 使用线性插值法计算精确温度：-1367
                // {30,8315},
                // {35,6948},
                start_temp = ntc_3380k_b_value[i - 1][0];                              // 获得文档起点值
                range_temp = ntc_3380k_b_value[i][0] - ntc_3380k_b_value[i - 1][0];    // 获得NTC这两个区间的总温度值5
                range_res = ntc_3380k_b_value[i][1] - ntc_3380k_b_value[i - 1][1];     // 获得NTC这两个区间的总电阻值-1367
                ntc_ratio = Rntc - ntc_3380k_b_value[i - 1][1];                        // 获得当前NTC电阻值在这个区间中的位置8067-8315=-248
                *p_temperature = start_temp + range_temp * ntc_ratio / range_res;  // 计算最终的温度值

                ret = 0;
            }
        }else{
            // DDL_Printf("NTC ADC converting!!\n");
        }
    }

    return ret;
}
#else
int ntc_adc_polling(int *p_temperature) {
    uint32_t AdcValue=m_au16AdcValue_adc2[0], Rntc;
    int ret = -1;
    int i;
    int start_temp, range_temp, range_res, ntc_ratio;

    /* Can ONLY start sequence A conversion.
       Sequence B needs hardware trigger to start conversion. */

    // Rntc=(adc_value*Rup)/(NTC_ADC_ACCURACY-adc_value)
    Rntc = (AdcValue * NTC_RUP_) / (NTC_ADC_ACCURACY - AdcValue);

    int rows = sizeof(ntc_3380k_b_value) / sizeof(ntc_3380k_b_value[0]);

    for (i = 0; i < rows; i++) {
        if (ntc_3380k_b_value[i][1] <= Rntc) {
            break;
        }
    }

    // DDL_Printf("The ADC value of potentiometer is %u, voltage is %u mV ,Rntc:%d,row:%d, i:%d\n", \
            //     AdcValue, NTC_ADC_CAL_VOL(AdcValue),\
            //     Rntc,rows,i);
    if (i) {
        // 在 ResistanceTable 中找到 R_T 所在的位置区间，即找到 R_T 介于 RTable[i] 和 RTable[i+1] 之间。
        // 使用线性插值法计算精确温度：-1367
        // {30,8315},
        // {35,6948},
        start_temp = ntc_3380k_b_value[i - 1][0];                            // 获得文档起点值
        range_temp = ntc_3380k_b_value[i][0] - ntc_3380k_b_value[i - 1][0];  // 获得NTC这两个区间的总温度值5
        range_res = ntc_3380k_b_value[i][1] - ntc_3380k_b_value[i - 1][1];   // 获得NTC这两个区间的总电阻值-1367
        ntc_ratio = Rntc - ntc_3380k_b_value[i - 1][1];                      // 获得当前NTC电阻值在这个区间中的位置8067-8315=-248
        *p_temperature = start_temp + range_temp * ntc_ratio / range_res;    // 计算最终的温度值

        ret = 0;
    }

    return ret;
}

#endif 